package com.example.wwk.graduationpractice.Adapter;

import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.wwk.graduationpractice.R;
import com.example.wwk.graduationpractice.Bean.ChatConsultBean;
import com.example.wwk.graduationpractice.EmotionUtils.EmotionUtils;
import com.example.wwk.graduationpractice.Globalutils.MyApplication;
import com.example.wwk.graduationpractice.EmotionUtils.SpanStringUtils;

import java.util.List;

/**
 * Created by wwk on 2017/3/14.
 */

public class ChatConsultAdapter  extends RecyclerView.Adapter<ChatConsultAdapter.ViewHolder> {

    private List<ChatConsultBean> mChatList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;
        TextView leftTime;
        TextView rightTime;

        public ViewHolder(View itemView) {
            super(itemView);
            leftLayout = (LinearLayout) itemView.findViewById(R.id.chat_consult_item_left_layout);
            rightLayout = (LinearLayout) itemView.findViewById(R.id.chat_consult_item_right_layout);
            leftMsg = (TextView) itemView.findViewById(R.id.chat_consult_item_left_msg);
            rightMsg = (TextView) itemView.findViewById(R.id.chat_consult_item_right_msg);
            leftTime = (TextView)itemView.findViewById(R.id.chat_consult_item_left_text_time);
            rightTime = (TextView)itemView.findViewById(R.id.chat_consult_item_right_text_time);
        }
    }
    public  ChatConsultAdapter(List<ChatConsultBean> chatList){
        mChatList = chatList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_consult_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ChatConsultBean chatConsult = mChatList.get(position);
        String msg = chatConsult.getContent();
        //消息具体内容
        SpannableString span ;

        if(chatConsult.getType() == ChatConsultBean.TYPE_RECEIVED){ //与bean中定义的常量比较，判断接收或发送消息
            //收到消息，显示左边的布局，隐藏右边的消息布局
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
             span = SpanStringUtils.getEmotionContent(EmotionUtils.EMOTION_CLASSIC_TYPE ,
                    MyApplication.getContext(),holder.leftMsg,msg);
            holder.leftMsg.setText(span);
            holder.leftTime.setText(chatConsult.getTime());              //获取系统时间
        }
        else if(chatConsult.getType()==ChatConsultBean.TYPE_SENT){
            //发送消息，显示右边新消息布局，隐藏左边的消息布局
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            span = SpanStringUtils.getEmotionContent(EmotionUtils.EMOTION_CLASSIC_TYPE,
                    MyApplication.getContext(),holder.rightMsg,msg);
            holder.rightMsg.setText(span);
            holder.rightTime.setText(chatConsult.getTime());
        }
    }

    @Override
    public int getItemCount() {
        return mChatList.size();
    }
}

